﻿using UnityEngine;
using System.Collections;

namespace gameover {
	public class backToIndex : MonoBehaviour {

		float waitTime = 0.5f;

		void OnMouseDown() {
			toIndexAfterSec (waitTime);
		}

		// Use this for initialization
		void Start () {
			// 播放fail
			AudioSource audioSource = GameObject.FindGameObjectWithTag("MainCamera").GetComponent<AudioSource>();
			audioSource.clip = Resources.Load("mp3/fail") as AudioClip;
			audioSource.Play();


			//
			object[] gameObjects;
			gameObjects = GameObject.FindSceneObjectsOfType(typeof(Transform));
			foreach (Transform go in gameObjects)
			{
				if(!go.tag.Equals("CANNOT_DESTROY")&&(!go.tag.Equals("MainCamera"))){
					Network.Destroy(go.networkView.viewID);
				}
			}
		}
		
		// Update is called once per frame
		void Update () {
			if (Input.GetKeyDown (KeyCode.J)) {
				toIndexAfterSec (waitTime);
			}
		}

		void toIndexAfterSec(float s) {
			StartCoroutine (toIndex (s));
		}

		// s秒后回到Index
		IEnumerator toIndex(float s) {
			yield return new WaitForSeconds(s);

			Application.LoadLevel("index");
		}
	}
}